/************************************************************************************
**  
* @copyright (c) 2015-, Technology Co., LTD. All Right Reserved.
*
*************************************************************************************/
/**
* @file     duye_cap_udp_service.h
* @version     
* @brief      
* @author   
* @date     2013-01-09
* @note 
*
*  1. 2018-01-09 created this file
* 
*/
#pragma once

#include <string>
#include <map>
#include <duye_type.h>

#include <duye_cap_filter_engine.h>
#include <duye_cap_service_base.h>
#include <duye_cap_packet.h>
#include <duye_cap_udp_session.h>
#include <duye_cap_netlayer_service.h>

namespace duye {

class UdpServiceFilterWorker : public FilterWorker {
public:
	UdpServiceFilterWorker() {}
	virtual ~UdpServiceFilterWorker() {}
	virtual bool filterPkt(const PacketReference& pktRef, const FilterRule& rule);
};

/**
 * @brief UDP listener interface
 */
class UdpListener : public DataServiceListener {
public:
	virtual ~UdpListener() {}
	virtual int32 onUdpPacket(const PacketReference& pktRef) = 0;
    virtual int32 onUdpSession(const PacketReference& pktRef, const UdpSession& udpSession) = 0;
};

typedef std::map<UdpListener*, FilterRule*> UdpListenerMap;

/**
 * @brief net layer server interface
 */
class UdpService : public CapDataService, public NetLayerListener, public Singleton<UdpService> {
public:
	UdpService();
	virtual ~UdpService();

	// implementation WdpiService interface 
	virtual bool start();
    // implementation WdpiService interface
	virtual bool stop();
	// implementation WdpiService interface
	virtual bool run();	
	// implementation CapDataService interface
    virtual bool addListener(DataServiceListener* listener, const FilterRule* filterRule = NULL);
	// implementation CapDataService interface
    virtual bool delListener(DataServiceListener* listener);

	// implementation NetLayerListener interface
	virtual int32 onNetLayerPacket(const PacketReference& pktRef);

private:
	const UdpSession& findUdpSession(const PacketReference& pktRef);
    
private:
    UdpListenerMap m_listenerMap;
	UdpServiceFilterWorker m_filterWorker;
};

}
